import { _decorator, Component, log, Node } from 'cc';
import { event_clear, event_dispatch, event_on, event_once, event_off } from '../../sgame/event/EventManager';
import { llog } from '../../sgame/logger/Logger';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {
    start() {
        console.warn("test listen");
        this.testListen();
        console.warn("test once");
        this.testOnce();
        console.warn("test unlisten");
        this.testUnlisten();
        console.warn("test unlistenall");
        this.testUnlistenAll();
        console.warn("test clear");
        this.testClear();
        console.warn("end test clear");
    }

    testListen() {
        event_on('test', (a, b) => {
            console.log("匿名函数", a, b);
        })

        event_on('test', this.listener, this);
        llog("event_dispatch('test', 1, 2)");
        event_dispatch('test', 1, 2);
    }

    testOnce() {
        event_once('testOnce', (a, b) => {
            console.log("匿名函数", a, b);
        })

        event_once('testOnce', this.listener, this);
        llog("event_dispatch('testOnce', 1, 2)");
        event_dispatch('testOnce', 1, 2);
        llog("event_dispatch('testOnce', 3, 4)");
        event_dispatch('testOnce', 3, 4);
    }

    testUnlisten() {
        let callback = (a, b) => {
            console.log("匿名函数", a, b);
        };

        event_on('testUnlisten', callback);
        event_on('testUnlisten', this.listener, this);

        // unlisten('test', this.listener, this);
        event_off('testUnlisten', callback);
        llog("event_dispatch('testUnlisten', 1, 2)");
        event_dispatch('testUnlisten', 1, 2);

        event_on('testUnlisten2', callback);
        event_on('testUnlisten2', this.listener, this);
        event_off('testUnlisten2');
        llog("event_dispatch('testUnlisten2', 1, 2)");
        event_dispatch('testUnlisten2', 1, 2);
    }

    testUnlistenAll() {
        let callback = (a, b) => {
            console.log("匿名函数", a, b);
        };

        event_on('testUnlistenAll', callback);

        event_on('testUnlistenAll', this.listener, this);
        event_off('testUnlistenAll');
        llog("event_dispatch('test', 1, 2)");
        event_dispatch('testUnlistenAll', 1, 2);
    }

    testClear() {
        let callback = (a, b) => {
            console.log("匿名函数", a, b);
        };

        event_on('test', callback);

        event_on('test', this.listener, this);
        event_clear();
        event_dispatch('test', 1, 2);
    }

    listener(a, b) {
        console.log("绑定了this为当前对象, this.node.name: ", this.node.name, `a: ${a}, b: ${b}`);
    }
}